#ifndef REMOTECORES_ERRORS_H_
#define REMOTECORES_ERRORS_H_

/* Based off of Proton's Error Objects, which are based on ETL's, which are
 * based on Subversion's. Blah Blah Blah
 */

#include <apr.h>
#include <apr_errno.h>
#include <apr_pools.h>

#define RC_ERR(expression) do { \
    remotecores_error_t *rc__err = (expression); \
    if (rc__err) \
        return rc__err; \
} while (0)

#define REMOTECORES_SUCCESS NULL

/** Exception Object */
typedef struct {
    apr_pool_t *pool;

    apr_status_t err;
    const char *msg;

    apr_uint32_t line;
    const char *file;
} remotecores_error_t;

#define remotecores_error_create(err, msg)  \
    remotecores_error_create_impl(err,      \
                                  msg,      \
                                  __LINE__, \
                                  __FILE__);

remotecores_error_t*
remotecores_error_create_impl(apr_status_t err,
                              const char *msg,
                              apr_uint32_t line,
                              const char *file);

#define remotecores_error_createf(err, fmt, ...) \
    remotecores_error_createf_impl(err, \
                                   __LINE__ \
                                   __FILE__ \
                                   fmt, \
                                   __VA_ARGS__)

remotecores_error_t*
remotecores_error_createf_impl(apr_status_t err,
                               apr_uint32_t line,
                               const char *file,
                               const char *fmt,
                               ...);

void remotecores_error_destroy(remotecores_error_t *err);

#endif
